package cn.mengfly.fxtest.app;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author Mengfly
 * @date 2021/5/26 23:26
 */
public class StartVsShownJavaFxApp extends Application {

    private final DoubleProperty startX = new SimpleDoubleProperty();
    private final DoubleProperty startY = new SimpleDoubleProperty();
    private final DoubleProperty shownX = new SimpleDoubleProperty();
    private final DoubleProperty shownY = new SimpleDoubleProperty();

    @Override
    public void start(Stage stage) throws Exception {
        Label startLabel = new Label("Start Dimensions");
        TextField startTf = new TextField();
        startTf.textProperty().bind(Bindings.format("(%.1f, %.1f)", startX, startY));

        Label shownLabel = new Label("Shown Dimensions");
        TextField shownTf = new TextField();
        shownTf.textProperty().bind(Bindings.format("(%.1f, %.1f)", shownX, shownY));

        GridPane gp = new GridPane();
        gp.add(startLabel, 0, 0);
        gp.add(startTf, 1, 0);

        gp.add(shownLabel, 0, 1);
        gp.add(shownTf, 1, 1);
        gp.setHgap(10);
        gp.setVgap(10);

        HBox hBox = new HBox(gp);
        hBox.setAlignment(Pos.CENTER);

        VBox vBox = new VBox(hBox);
        vBox.setAlignment(Pos.CENTER);

        Scene scene = new Scene(vBox, 480, 320);
        stage.setScene(scene);

        // before show()... I just set this to 480x320, right?
        startX.set(stage.getWidth());
        startY.set(stage.getHeight());

        stage.setOnShown(windowEvent -> {
            shownX.set(stage.getWidth());
            shownY.set(stage.getHeight());
            // all available now
        });

        stage.setTitle("Start Vs. Shown");
        stage.show();

    }




}
